package com.ronglian.ucdisk.share;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;


/**
 * 
 * @author baipeng 
 * 定位于 直接与 数据库交互的对象
 */
@Entity
@Table(name="share_event")
public class ShareEvent {
	
	@Id
	public String id; 
	public String shareTitle; 
	public Date endTime; 
	
	public String senderId; 
	public String senderName;
	
	//参数 
	@Transient @JsonIgnore  private String nodeIdList = "";
	

	@Transient @JsonIgnore  private String receiverIdList = "";
	@Transient @JsonIgnore  private String recTeamIdList = "";
	
	//返回值
	@Transient public List<HashMap<String,String>> nodeList;
	@Transient public List<HashMap<String,String>> receiverList ; 
	@Transient public List<HashMap<String,String>> receiverTeamList;
	
	public String type ;
	public String permission;
	
	
	public String code ; 
	public String url ; 
	public String shareBatch;
	public Date createTime;
	public Date lastUpdateTime ;
	
	public String parentShareId;
	
	
	@JsonIgnore public String status = "active"; // omit,active
	
	@OneToMany(
			orphanRemoval=true,
			fetch = FetchType.EAGER , 
			mappedBy = "event" ,   
			cascade = { CascadeType.PERSIST ,CascadeType.REMOVE} )
	@JsonIgnore private List<ShareNodeEntity> shareNodeIdList ;
	

	

	@OneToMany(orphanRemoval=true,fetch = FetchType.LAZY , mappedBy= "event" ,   cascade ={  CascadeType.PERSIST ,CascadeType.REMOVE} )
	@JsonIgnore /*transient*/ private List<ShareReceiverEntity> shareReceiverIdList;
	

	@Transient public void buildList() {
		shareNodeIdList = new ArrayList<ShareNodeEntity>();
		if(nodeIdList == null ) nodeIdList = "";
		String []nodeIds = nodeIdList.split(",");
		for(String nid:nodeIds){
			nid = nid.trim();
			if(nid.equals("")) continue; 
			ShareNodeEntity sne = new ShareNodeEntity(); 
			sne.setId( UUID.randomUUID().toString() ); 
			sne.setNodeId( nid ) ;
			sne.setEvent( this ) ;
			shareNodeIdList.add(sne);
			
		}
		shareReceiverIdList = new ArrayList<ShareReceiverEntity>();
		String []rids = receiverIdList.split(",");
		for(String rid : rids) {
			rid = rid.trim();
			if(rid.equals("")) continue; 
			ShareReceiverEntity sre = new ShareReceiverEntity() ;
			sre.setId( UUID.randomUUID().toString() ) ;
			sre.setReceiverId(rid ) ;  
			sre.setEvent(this); 
			sre.setReceiverType("single" ) ;
			shareReceiverIdList.add(sre);
		}
		
		String []tids = recTeamIdList.split(",");
		for(String tid :tids) {
			tid  = tid.trim() ; 
			if(tid.equals("")) continue; 
			ShareReceiverEntity sre = new ShareReceiverEntity() ;
			sre.setId ( UUID.randomUUID().toString() ) ;
			sre.setReceiverId( tid ) ;
			sre.setEvent  (  this ) ; 
			sre.setReceiverType( "team" ) ;
			shareReceiverIdList.add(sre);
		}
	}
	public void addResponseNodeInfo(String id,String name){
		if(nodeList == null) {
			nodeList =  new ArrayList<HashMap<String,String>>();  
		}
		HashMap<String,String> map = new HashMap<String,String>() ; 
		
		map.put("id", id);
		map.put("name", name);
		nodeList.add(map);
	}
	public void addResponseReceiverInfo(String id,String name){
		if(receiverList == null ) {
			receiverList = new ArrayList<HashMap<String,String>>();
		}
		HashMap<String,String> map = new HashMap<String,String>() ; 
		
		map.put("id", id);
		map.put("name", name);
		receiverList.add(map);
	}
	public void addResponseReceiverTeamInfo(String id,String name) {
		if(receiverTeamList == null ) {
			receiverTeamList = new ArrayList<HashMap<String,String>>(); 
		}
		HashMap<String,String> map = new HashMap<String,String>() ; 
		
		map.put("id", id);
		map.put("name", name);
		receiverTeamList.add(map);
	}
	/**
	 * 根据 shareNodeIdList ,获取 相关的name，并返回
	 * 用于向客户端返回ShareEvent信息的情况 
	 * 查看详细信息的时候才进行设置
	 */
	public void buildResponseNodeList() {
		//do nothing  . 放在 Service中处理
	}
	/**
	 * 根据 shareReceiverIdList, 获取相关信息返回
	 */
	public void buildResponseReceiverList(){
		//do nothing .放在 Service中处理 
	}
	public void buildResponseReceiverTeamList() {
		//do nothing now. 放在 Service中处理
	}
	
	public List<ShareNodeEntity> getShareNodeIdList() {
		return shareNodeIdList;
	}

	public void setShareNodeIdList(List<ShareNodeEntity> shareNodeIdList) {
		this.shareNodeIdList = shareNodeIdList;
	}

	public List<ShareReceiverEntity> getShareReceiverIdList() {
		return shareReceiverIdList;
	}

	public void setShareReceiverIdList(List<ShareReceiverEntity> shareReceiverIdList) {
		this.shareReceiverIdList = shareReceiverIdList;
	}
	
	public String getNodeIdList() {
		return nodeIdList;
	}
	public void setNodeIdList(String nodeIdList) {
		this.nodeIdList = nodeIdList;
	}
	public String getReceiverIdList() {
		return receiverIdList;
	}
	public void setReceiverIdList(String receiverIdList) {
		this.receiverIdList = receiverIdList;
	}
	public String getRecTeamIdList() {
		return recTeamIdList;
	}
	public void setRecTeamIdList(String recTeamIdList) {
		this.recTeamIdList = recTeamIdList;
	}


	
}
